package com.sike.netty.im.server;


import com.sike.netty.im.common.codc.CommandBodyDecoder;
import com.sike.netty.im.common.codc.MessageCodec;
import com.sike.netty.im.common.codc.ProtocolFrameDecoder;
import com.sike.netty.im.server.handler.ImIdleStateHandler;
import com.sike.netty.im.server.handler.*;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * im 服务器启动类
 *
 * @author chenssy
 * @date 2023/09/14
 */
public class ImServerStarter {
    public static void main(String[] args) throws Exception {
        ServerBootstrap bootstrap = new ServerBootstrap()
                .group(new NioEventLoopGroup(),new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
//                        ch.pipeline().addLast(new LoggingHandler(LogLevel.DEBUG));
                        ch.pipeline().addLast(new ImIdleStateHandler());
                        ch.pipeline().addLast(new ProtocolFrameDecoder());
                        ch.pipeline().addLast(new MessageCodec());
                        ch.pipeline().addLast(new CommandBodyDecoder());
                        ch.pipeline().addLast(new HearBeatTimerRequestHandler());
                        ch.pipeline().addLast(new LoginHandler());
                        ch.pipeline().addLast(new AuthHandler());
                        ch.pipeline().addLast(new OneChatRequestHandler());
                        ch.pipeline().addLast(new CreateGroupRequestHandler());
                        ch.pipeline().addLast(new ServerExceptionHandler());
                        ch.pipeline().addLast(new JoinGroupRequestHandler());
                        ch.pipeline().addLast(new ListGroupMemebersRequestHandler());
                        ch.pipeline().addLast(new ExitGroupRequestHandler());
                        ch.pipeline().addLast(new GroupChatRequestHandler());
                    }
                });

        Channel channel = bootstrap.bind(8801).sync().channel();
    }
}
